{block name=body}


<div class="row">
	<div class="col-sm-12">
			<h2 class="title">沟通</h2>
			<form class="form" id="FilterClassesList-form" role="form" accept-charset="UTF-8" action="" method="post">
				<input type="hidden" value="FilterClassesByTerm" name="action"/> 
				<input type="hidden" value="/admin/includes/processes/processes-enrolment.php" name="url"/> 
				<input type="hidden" name="formToken" value="{$token}" />

				<div class="form-group col-sm-12 reports-admin" style="border-right:none; padding: 15px 20px 25px;">
					<label for="term_object_id">选择学期:</label><br>
                    <select class="select-dropdown" name="term_object_id" id="term_object_id">
                        {foreach $terms as $term} 
                            <option value="{$term.term_object_id}" >{$term.term_name}</option>
                        {/foreach}
                    </select>
				</div>
			</form>
	</div>
</div>

<div class="row">
	<div class="col-sm-12">
				<div id="email-form-wrapper" class="form-wrapper" >
					<form class="form-horizontal" id="send-email-classes-form" role="form" accept-charset="UTF-8" action="" method="post">
						<input type="hidden" value="SendEmailtoClasses" name="action"/> 
						<input type="hidden" value="/admin/includes/processes/processes-email.php" name="url"/> 
  					<input type="hidden" name="formToken" value="{$token}" />
  					<input type="hidden" name="term_object_id" id="sel_term_object_id" value="{$terms.0.term_object_id}" />

						<div id="classterms-wrapper">
							{include file="classterm-tree-list.tpl"}
						</div>
						<div class="form form-group">
							<label class="col-sm-3 control-label" id="methodlabel">方式*</label>
							<div class="col-sm-2">
								<input type="radio" name="method" value="email" id="method_email" checked="checked">
								<label for="method_email">邮件</label>
							</div>
							<div class="col-sm-3">
								<input type="radio" name="method" value="sms" id="method_sms">
								<label for="method_sms">短信</label>
							</div>
						</div>
						<div class="form-group method_sms" style="display:none;">
							<label for="sms_content" class="col-sm-3 control-label" >消息*</label>
							<div class="col-sm-5">
								<textarea class="form-control remove-content req-sms" maxlength="160" rows="4" name="sms_content"></textarea>
							</div>
						</div>
						<div class="form form-group method_email">
							<label for="subject" class="col-sm-3 control-label">主题*</label>
							<div class="col-sm-6">
								<input class="form-control remove-content req-email" type="text" name="subject" value="" required>
							</div>
						</div>
						<div class="form-group method_email">
							<label for="content" class="col-sm-3 control-label" >消息*</label>
							<div class="col-sm-5">
								<textarea name="content" class="tinymce remove-content req-email" required>会员您好： &lt;br&gt; &lt;br&gt; 致以亲切的问候，我们是澳睿跑团队。</textarea>
							</div>
						</div>
						<div class="col-sm-offset-3 error-alert" style="display:none;">
	          	<div class="alert alert-danger fade in ">
								<button class="close" aria-hidden="true" type="button" onclick="$(this).closest('.error-alert').fadeOut('slow');">&times;</button>
								<strong></strong>
							</div>
	          </div>
						<div class="text-center">
							<button type="button" class="btn btn-warning method_email" onclick="PreviewEmail('send-email-classes-form')">Preview</button>
							<button type="submit" class="btn btn-default">发送</button>
						</div>
					</form>
	  		</div>
	</div>
</div>
{/block}

{block name=tail}
	<script type="text/javascript">
	
		$(function() {
			$('#send-email-classes-form').validate({
				onkeyup: false
			});
			
			$('#FilterClassesList-form').validate();
			$('#term_object_id').change(function(){
				$('#FilterClassesList-form').submit();
				$('#sel_term_object_id').val( $(this).val() );
			});

			$('input[name="method"]').change(function(){
				if($(this).val() == 'sms' ){
					$('.method_email').hide('slow');
					$('.method_sms').show('slow');
					$('.req-email').removeAttr('required');
					$('.req-sms').attr('required','required'); 
				}else{
					$('.method_sms').hide('slow');
					$('.method_email').show('slow');
					$('.req-sms').removeAttr('required');
					$('.req-email').attr('required','required'); 
				}
			});
		});


		var running = false;
		function PreviewEmail(formID){
			if(!running && $('#'+formID).valid()){
				running = true;
				var datastring = $('#'+formID).serialize();
				$('body').css('cursor', 'wait');
				var query = $("#query").val();
				$.ajax({
					type : "POST",
					url : "/admin/includes/processes/view-email.php",
					cache : false,
					data : datastring,
					dataType : "html",
					success : function(data, textStatus) {
						try {
							var obj = $.parseJSON(data);
							if (obj.error) {
								$('#error').find('b').html(obj.error);
								$('#error').slideDown();
								setTimeout(function(){
									$('#error').slideUp();
						    	},10000);
						 	} else if (obj.success){
						 		window.open("/admin/includes/processes/view-email.php?action=PreviewEmail");
						 	}
						} catch (err) {}
						running = false;
						$('body').css('cursor', 'default');
					},
					error: function(){
						$('body').css('cursor','default'); 
						console.log('AJAX error');
				  }
				});
			}
		}
	</script>

{/block}
	